<?php
/* @var $this ProjectController */
/* @var $model Project */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'project-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>true,
)); ?>


	<?php echo $form->errorSummary($model); ?>

	
	<div class="row">
		<?php echo $form->labelEx($model,'year'); ?>
		<?php echo CHtml::activeDropDownList($model,'year',DateTimeArray::yearThaiArray()); ?>
		<?php echo $form->error($model,'year'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'semester'); ?>
		<?php echo CHtml::activeDropDownList($model,'semester',array('1'=>'1','2'=>'2'),array('prompt'=>'ภาคการศึกษา')); ?>
		<?php echo $form->error($model,'semester'); ?>
	</div>
	

	<div class="row">
		<?php echo $form->labelEx($model,'projectName'); ?>
		<?php echo $form->textField($model,'projectName',array('size'=>60,'maxlength'=>512)); ?>
		<?php echo $form->error($model,'projectName'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'projectEngName'); ?>
		<?php echo $form->textField($model,'projectEngName',array('size'=>60,'maxlength'=>512)); ?>
		<?php echo $form->error($model,'projectEngName'); ?>
	</div>
	<div class="row">
		<b>ชื่อ-สกุล </b><?php echo $form->textField($regStudent,'student_name',array('size'=>30,'maxlength'=>60,'readonly'=>'readonly')); ?>
		<?php echo $form->error($regStudent,'student_name'); ?>
		<b>รหัสนักศึกษา </b><?php echo $form->textField($regStudent,'student_id',array('size'=>20,'maxlength'=>20,'readonly'=>'readonly')); ?>
		
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchStudent',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
					addStudent();
					$("#dialogStudent").dialog("open"); return false;
				}'),
		));
		?> <font color="red">*</font>
		<?php echo $form->error($student,'studentID'); ?>
	</div>
	<div class="row">
		<!--  <b>ระดับปริญญา </b><?php echo CHtml::textField('level','ปริญญาโท',array('readonly'=>'readonly')); ?>--><b> สาขาวิชา </b><?php echo CHtml::telField('major',$regStudent->major,array('readonly'=>'readonly')); ?>
	</div>
	<div class="row">
		<!--<b>แขนงวิชา </b><?php echo CHtml::textField('branch','',array('readonly'=>'readonly')); ?>--><b> ภาควิชา </b><?php echo CHtml::telField('department',$regStudent->department,array('readonly'=>'readonly')); ?>
	</div>

	<div class="row">
		
		<b>ลงทะเบียนมาแล้ว  </b> <?php echo CHtml::textField('credit',$regStudent->sum_credit,array('size'=>8,'readonly'=>'readonly')); ?> <b>หน่วยกิต</b>  <b>เกรดเฉลี่ยสะสม  </b> <?php echo CHtml::textField('gradAVG',$regStudent->gpa,array('size'=>8,'readonly'=>'readonly')); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'projectCredit'); ?>
		<?php echo $form->textField($model,'projectCredit',array('size'=>4,'maxlength'=>4)); ?>
		<?php echo $form->error($model,'projectCredit'); ?>
	</div>
	<div class="row">
	
	<b>อาจารย์ที่ปรึกษาหลัก  </b><?php echo CHtml::textField('majorAdvisor',$teacher1->name,array('readonly'=>'readonly')); ?>
	<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchMajorTeacher',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
				addTeacher(1);
				//$("#dialogTeacher").dialog("destroy");
 				$("#dialogTeacher").dialog("open"); return false;}'),
		));
		?> <font color="red">*</font>
		<?php echo CHtml::hiddenField('majorAdvisorID',$teacher1->user_id)?>
	</div>
	<div class="row">
	<b>อาจารย์ที่ปรึกษารอง  </b><?php echo CHtml::textField('minorAdvisor',$teacher2->name,array('readonly'=>'readonly')); ?>
	<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnSearchMinorTeacher',
				'caption'=>'ค้นหา',
				'onclick'=>new CJavaScriptExpression('function(){
				addTeacher(2);
				//$("#dialogTeacher").dialog("destroy");
 				$("#dialogTeacher2").dialog("open"); return false;}'),
		));
		?> 
		<?php 
		$this->widget('zii.widgets.jui.CJuiButton',array(
				'buttonType'=>'button',
				'name'=>'btnClearMinorTeacher',
				'caption'=>'ยกเลิก',
				'onclick'=>new CJavaScriptExpression('function(){
				//$("#dialogTeacher").dialog("destroy");
 				$("#minorAdvisorID").val("");
				$("#minorAdvisor").val("");
				 return false;}'),
		));
		?>
		<font color="red">*</font>
		<?php echo CHtml::hiddenField('minorAdvisorID',$teacher2->user_id)?>
	</div>
	

	<div class="row">
		<?php echo $form->labelEx($model,'startDate'); ?>
		<?php 
	
	$this->widget('zii.widgets.jui.CJuiDatePicker',array(
			//'name'=>'Document[docDate]',
			'model'=>$model,
			'attribute'=>'startDate',
			'language'=>'th',
			// additional javascript options for the date picker plugin
			'options'=>array(
					//'mode'=>'focus',
					//'dateFormat'=>'d MM, yy',
					//'showOn'=>'button', // 'focus', 'button', 'both'
					'dateFormat' => 'dd/mm/yy',
					//'value'=>strftime('%d/%m/%Y', strtotime($model->docDate)),
					'showAnim'=>'fold',
					'changeMonth'=> true,
					'changeYear'=> true,

			),
			'htmlOptions'=>array(
					'style'=>'height:20px;',
			
			),
	));
	?> dd/mm/YYYY
		<?php echo $form->error($model,'startDate'); ?>
	</div>

	

	

	<div class="row buttons">
	<?php 
	$this->widget('zii.widgets.jui.CJuiButton',array(
			'buttonType'=>'submit',
			'name'=>'btnSubmit',
			'value'=>'1',
			'caption'=>$model->isNewRecord ? 'บันทึกข้อมูล' : 'แก้ไขข้อมูล',
			'htmlOptions'=>array('class'=>'ui-button-primary'),
	)
	);
	?>

	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
	<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogStudent',
    'options'=>array(
        'title'=>'ค้นหานักษึกษา',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>630,
		'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
		),

    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogTeacher',
    'options'=>array(
        'title'=>'ค้นหาอาจารย์ที่ปรึกษาหลัก',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
		'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogTeacher2',
    'options'=>array(
        'title'=>'ค้นหาอาจารย์ที่ปรึกษารอง',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="divForForm"></div>
 
<?php $this->endWidget();?>
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogError',
    'options'=>array(
        'title'=>'เกิดข้อผิดลาพ',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
'buttons' => array(
		array('text'=>'ปิด','click'=> 'js:function(){$(this).dialog("close");}'),
),
    ),
));?>
<div class="errorForm"></div>
 
<?php $this->endWidget();?>
<script type="text/javascript">
// here is the magic
function addStudent()
{
	
    <?php echo CHtml::ajax(array(
            'url'=>array('regStudent/listStudent'),
            'type'=>'get',
            'success'=>"function(data)
            {
                    $('#dialogStudent div.divForForm').html(data);

            } ",
            ))?>;
    return false; 
 
}
 function getStudent(id)
 {	 
	 $(function () {
		
		 $.getJSON('<?=Yii::app()->createUrl('regStudent/getStudent')?>/'+id,function(data)
	            {
			 			$('#RegStudent_student_id').val(data['studentID']);
	                    $('#RegStudent_student_name').val(data['name']);
	                    $('#RegStudent_student_name').val(data['name']);
	                    $('#major').val(data['major']);
	                    $('#department').val(data['department']);
	                    $('#credit').val(data['credit']);
	                    $('#gradAVG').val(data['gradAVG']);
	                    
	            } );
	 } );
		 
 }
 function addTeacher(id)
 {
      $.ajax({'url':'<?=Yii::app()->createUrl('regTeacher/listTeacher')?>/'+id,'type':'get','success':function(data)
            {
          			if (id==1){
                    $('#dialogTeacher div.divForForm').html(data);
          			}else if(id==2){
          				$('#dialogTeacher2 div.divForForm').html(data);
              			}
          			

            } ,'cache':false});
     return false; 
  
 }
  function getTeacher(id,type)
  {	 
 	 $(function () {
 		
 		 $.getJSON('<?=Yii::app()->createUrl('regTeacher/getTeacher')?>/?id='+id,function(data)
 	            {
	            if (type==1){
	            	 if (data['teacherID']==$('#minorAdvisorID').val()) {
	            		 $('.errorForm').html('<br><br><div align="center">เลือกอาจารย์ซ้ำ</div>');
			            	$('#dialogError').dialog("open");
	            	 }else{
 			 			$('#majorAdvisorID').val(data['teacherID']);
 	                    $('#majorAdvisor').val(data['name']);
	            	 }

	            }else if (type==2){
		            if (data['teacherID']==$('#majorAdvisorID').val()) {
		            	$('.errorForm').html('<br><br><div align="center">เลือกอาจารย์ซ้ำ</div>');
		            	$('#dialogError').dialog("open");
		            }else{
		            	$('#minorAdvisorID').val(data['teacherID']);
	                    $('#minorAdvisor').val(data['name']);
			            }
		            }
 	                    
 	            } );
 	 } );
 		 
  }
</script>